package com.huawei.hwclouds.lubanops.demo.common.client;

import java.util.HashMap;
import java.util.Map;

/**
 * @author y00438022
 * @since 2020/11/8
 **/
public abstract class AbstractHttpClient implements HttpClient {

    private int timeout = 5000; //默认超时时间

    private HeaderFactory headerFactory;

    public AbstractHttpClient(int timeout) {
        this.timeout = timeout;
    }

    public AbstractHttpClient(int timeout, HeaderFactory headerFactory) {
        this.timeout = timeout;
        this.headerFactory = headerFactory;
    }

    @Override
    public HttpCallResult sendGet(String url, Map<String, ?> params, Map<String, String> headers, int timeout,
        String proxyHost, int proxyPort) {
        Map<String, String> allHeaders = makeHeaders(headers, headerFactory);
        return executeSendGet(url, params, allHeaders, timeout, proxyHost, proxyPort);
    }

    @Override
    public HttpCallResult sendGet(String url, Map<String, ? extends Object> params, Map<String, String> headers,
        int timeout) {
        return sendGet(url, params, headers, timeout, null, 0);
    }

    @Override
    public HttpCallResult sendGet(String url, Map<String, ? extends Object> params) {
        return sendGet(url, params, null);
    }

    @Override
    public HttpCallResult sendGet(String url, Map<String, ? extends Object> params, Map<String, String> headers) {
        return sendGet(url, params, headers, this.timeout);
    }

    @Override
    public HttpCallResult sendDelete(String url, Map<String, String> headers, int timeout, String proxyHost,
        int proxyPort) {
        Map<String, String> allHeaders = makeHeaders(headers, headerFactory);
        return executeSendDelete(url, allHeaders, timeout, proxyHost, proxyPort);
    }

    @Override
    public HttpCallResult sendDelete(String url, Map<String, String> headers, int timeout) {
        Map<String, String> allHeaders = makeHeaders(headers, headerFactory);
        return executeSendDelete(url, allHeaders, this.timeout, null, 0);
    }

    @Override
    public HttpCallResult sendDelete(String url, Map<String, String> headers) {
        return sendDelete(url, headers, this.timeout, null, 0);
    }

    @Override
    public HttpCallResult sendDelete(String url) {
        return sendDelete(url, null);
    }

    @Override
    public HttpCallResult sendPostJSON(String url, Map<String, String> headers, String content, int timeout,
        String proxyHost, int proxyPort) {
        Map<String, String> allHeaders = makeHeaders(headers, headerFactory);
        return executeSendPostJSON(url, allHeaders, content, timeout, proxyHost, proxyPort);
    }

    @Override
    public HttpCallResult sendPostJSON(String url, Map<String, String> headers, String content, int timeout) {
        return sendPostJSON(url, headers, content, timeout, null, 0);
    }

    @Override
    public HttpCallResult sendPostJSON(String url, Map<String, String> headers, String content) {
        return sendPostJSON(url, headers, content, this.timeout, null, 0);
    }

    @Override
    public HttpCallResult sendPutJSON(String url, Map<String, String> headers, String content, int timeout,
        String proxyHost, int proxyPort) {

        Map<String, String> allHeaders = makeHeaders(headers, headerFactory);
        return executeSendPutJSON(url, allHeaders, content, timeout, proxyHost, proxyPort);
    }

    @Override
    public HttpCallResult sendPutJSON(String url, Map<String, String> headers, String content, int timeout) {
        return sendPutJSON(url, headers, content, this.timeout, null, 0);
    }

    @Override
    public HttpCallResult sendPutJSON(String url, Map<String, String> headers, String content) {
        return sendPutJSON(url, headers, content, timeout);
    }

    /**
     * 执行http的get调用，子类来实现
     *
     * @param url
     * @param params
     * @param headers
     * @param timeout
     * @return
     */
    public abstract HttpCallResult executeSendGet(String url, Map<String, ? extends Object> params,
        Map<String, String> headers, int timeout, String proxyHost, int proxyPort);

    /**
     * 执行http的delete调用，子类来实现
     *
     * @param url
     * @param headers
     * @param timeout
     * @return
     */
    public abstract HttpCallResult executeSendDelete(String url, Map<String, String> headers, int timeout,
        String proxyHost, int proxyPort);

    /**
     * 执行http的post调用,子类来实现
     *
     * @param url
     * @param headers
     * @param timeout
     * @return
     */
    public abstract HttpCallResult executeSendPostJSON(String url, Map<String, String> headers, String content,
        int timeout, String proxyHost, int proxyPort);

    /**
     * 执行http的put调用,子类来实现
     *
     * @param url
     * @param headers
     * @param timeout
     * @return
     */
    public abstract HttpCallResult executeSendPutJSON(String url, Map<String, String> headers, String content,
        int timeout, String proxyHost, int proxyPort);

    /**
     * 将headerfactory合并到headers中起来
     *
     * @param headers
     * @param headerFactory
     * @return
     */
    public Map<String, String> makeHeaders(Map<String, String> headers, HeaderFactory headerFactory) {
        Map<String, String> totalHeaders = new HashMap<>();

        if (headers != null) {
            totalHeaders.putAll(headers);
        }

        if (headerFactory != null) {
            Map<String, String> additionalHeader = headerFactory.makeHeader();
            if (additionalHeader != null && !additionalHeader.isEmpty()) {
                totalHeaders.putAll(additionalHeader);
            }
        }

        return totalHeaders;
    }

}
